// Remote_PsoEp3.cpp : Defines the entry point for the console application.
//
#define _CRT_SECURE_NO_WARNINGS
#include "../../Shared/Shared.h"
#include "stdafx.h"
#ifdef _DEBUG
	#define VERSION "Remote Pso Ep3 --->(DEBUG)<----"
#else
	#define VERSION "Remote Pso Ep3"
#endif

int _tmain(int argc, char *argv[])
{
	string Command;
	int Button;
	bool takescreen = true;
	bool ScreenParam = false;
	bool DebugPressing = false;
#ifdef _DEBUG
	DebugPressing = true;
#endif
	Controller Pad;
	for(int i=1; i < argc; i++)
	{
#ifdef _DEBUG
		cout << "Showing argument nr. " << i << " : " << argv[i] << "\n";
#endif
		if ((argv[i] = "-s") || (argv[i] = "-S") )
		{
			ScreenParam = true;
		}
		else if ((argv[i] = "-d") || (argv[i] = "-D") )
		{
			DebugPressing = true;
		}
#ifdef _DEBUG
		else
		{
			cout << "unknown startup parameter " << argv[i] << " ";
		}
#endif
	}
	cout << "\n--------------------------------------------------------------------------------";
	cout << "Welcome to Daco's " << VERSION << " \n";
	cout << "Credits go to parasite for making Gcnrd and Opeth/my bro for C++ help\n";
	cout << "--------------------------------------------------------------------------------\n";
	cout << "\n";
	//this was just a test to see how button pressing was done. and to check value's of keys
	if(DebugPressing)
	{
		int test;
		cout << "Press Keys to see there value\nPress esc to quit the key pressing test\n";
		while(1)
		{
			if(_kbhit())
			{
				test = _getch();
				if(test == 27)
					break;
				else
					printf("key %c (int value %i) is pressed\n",test,test);
			}
		}
	}
	cout << "Load Gcnrd/GcnrdGUI using loader and press a key when pso has restarted.\n";
	Pause();
	cout << "Loading settings...\n";
	Pad = LoadSettings();
	cout << "Taking first screenshot...\n";
	try
	{
		ShellExecuteA( NULL, "Open", "/gcnrd.exe", "screen xfb screen.bmp", NULL, SW_HIDE );
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
	}
	if(ScreenParam)
		cout << "future screens will not be taken\n";
	cout << "\n\nlook at screen.bmp to see what your cube is doing ;P\n";
	Sleep(1000);
#ifdef _WIN32
	//TODO: find a way for Linux to open the screenshot :/
	ShellExecuteA(NULL,"Open","screen.bmp","explorer.exe",NULL,SW_HIDE );
#endif
	cout << "Press Button or Press esc to enter a Command\n";
	while(1)
	{
		if(_kbhit())
		{
			Button = _getch();
			if (Button == 27)
			{
				cout << "enter the command:\n";
				cin >> Command;
				if(Command == "exit")
				{
					return 0;
				}
				else if (Command == "screen")
				{
					takescreen = false;
				    ShellExecuteA( NULL, "Open", "/gcnrd.exe", "screen xfb screen.bmp", NULL, SW_HIDE );
				    cout << "Took new screen\n\n";
				}
				//List of gcnrd commands
				else if(Command == "combo")
				{
					string CCommand;
					string CoCommand;
					cout << "please enter the button/analoge value [a good one lol] \nby default(no action) 0000FF01 will be set\n";
					cin >> CoCommand;
					CCommand = "poke 8053D740 " + CoCommand;
					cout << "value to poke: " << CoCommand;

					ShellExecuteA( NULL, "open", "/gcnrd.exe", CCommand.c_str(), NULL, SW_HIDE );
				}
				else if(Command == "run")
				{
					ShellExecuteA( NULL, "Open", "/gcnrd.exe", "run", NULL, SW_HIDE );
					cout << "now running the gamecube...\n";
				}
				else if(Command == "pause")
				{
					ShellExecuteA( NULL, "Open", "/gcnrd.exe", "pause", NULL, SW_HIDE );
					cout << "now pausing the gamecube...\n";
					takescreen = false;
				}
			}
			//End commands and start buttons
			else if(Button == Pad.Start){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 1000FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.A){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0100FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.B){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0200FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.Y){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0800FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.X){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0400FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.L){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "next", NULL, SW_HIDE );
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0040FF00", NULL, SW_HIDE );
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D74C 0000C502", NULL, SW_HIDE );
				Sleep(600);
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "run", NULL, SW_HIDE );
			}
			else if(Button == Pad.R){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "next", NULL, SW_HIDE );
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0020FF00", NULL, SW_HIDE );
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D74C 00FF03CE", NULL, SW_HIDE );
				Sleep(600);
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "run", NULL, SW_HIDE );
			}
			else if(Button == Pad.Z){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0010FF00", NULL, SW_HIDE );
			}

			//list of Analoge stick and D-pad
			else if(Button == Pad.ApadUp){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0000046E", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadDown){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 00000298", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadLeft){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 00009B05", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadRight){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 00006902", NULL, SW_HIDE );				
			}
			else if(Button == Pad.ApadUpLeft){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0000C25B", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadUpRight){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0000435C", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadDownLeft){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0000B7BA", NULL, SW_HIDE );
			}
			else if(Button == Pad.ApadDownRight){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 00004CB2", NULL, SW_HIDE );
			}

			//list of DPad 
			else if(Button == Pad.DpadUp){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0008FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.DpadDown){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0004FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.DpadLeft){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0001FF00", NULL, SW_HIDE );
			}
			else if(Button == Pad.DpadRight){
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "poke 8053D740 0002FF00", NULL, SW_HIDE );
			}
			else
			{
				takescreen = false;
				cout << "invalid button\n";
#ifdef _DEBUG
				cout << "Button = " << Button << "\n";
#endif
			}
			if((takescreen) && (!ScreenParam))
			{
				ShellExecuteA( NULL, "Open", "/gcnrd.exe", "screen xfb screen.bmp", NULL, SW_HIDE );
				cout << "Took new screen\n";
			}
			takescreen = true;
			}
		}
	return 0;
}